<?php

namespace app\admin\model;

use think\Model;


class Usercoupon extends Model
{

    

    

    // 表名
    protected $name = 'user_coupon';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'create_time_text',
        'status_text'
    ];
    

    
    public function getStatusList()
    {
        return ['unuse' => __('Unuse'), 'used' => __('Used'), 'invalid' => __('Invalid')];
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function coupon()
    {
        return $this->belongsTo('Shopcoupon', 'coupon_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function checkCoupon($roomId, $user_id,$bookStart,$bookEnd,$amount)
    {
        $tuanList = $this->with(['coupon'])->where('Usercoupon.status', 'unuse')->where('user_id', $user_id)->select();
        $list = array();
        $day = date("w",$bookStart);



        if($day == 0){
            $day = 7;
        }
        foreach ($tuanList as $key => $val) {

            $tuanList[$key]['create_time_txt'] = date("Y-m-d",$val['create_time']);
            $tuanList[$key]['valid_time_txt'] = date("Y-m-d",$val['valid_time']);


            $rule = json_decode($val['coupon']['rule'], true);
            $time = $rule['time'];
            $week = $rule['week'];
            $roomList = $rule['room'];

            $start = explode("~", $time)[0];
            $end = explode("~", $time)[1];

            $startTime = strtotime(date("Y-m-d", $bookStart) . " " . $start);
            $endTime = strtotime(date("Y-m-d", $bookEnd) . " " . $end);
            if($endTime <$startTime){
                $endTime = strtotime(date("Y-m-d", strtotime("+1 day")) . " " . $end);
            }
            $isTime = false;


            if ($bookStart >= $startTime && $bookEnd <= $endTime) {
                $isTime = true;
            }
            if($start == "00:00" && $end == "23:59"){
                $isTime = true;
            }

            $isAmount = false;
            if($amount >= $val['coupon']['order_amount']){
                $isAmount = true;
            }



            $isWeek = false;
            foreach ($week as $k2 => $v2) {
                if ($v2 == $day) {
                    $isWeek = true;
                    break;
                }
            }

            foreach ($roomList as $k1 => $v1) {
                if ($v1 == $roomId) {
                    if ($isWeek == true && $isTime == true && $isAmount == true) {
                        $list[] = $val;
                    }
                }
            }


        }

        return $list;
    }

}
